package tawai;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
 * MIDlet of TawAI game.<br>
 * The entry point for the application<br>
 * Main menu is created here
 * @author Alex
 */
public class TawaiMIDlet extends MIDlet {

    private static Display display;
    private static TawaiMain canvas;
    private static MenuCanvas mainMenu;
    /**
     * Constructor<br>
     * Initializing is done here
     */
    public TawaiMIDlet() throws IOException {
        
        display = Display.getDisplay(this);
        canvas = new TawaiMain();

        // Main menu is implemented via Canvas
        mainMenu = new MenuCanvas();
        display.setCurrent(mainMenu);
        mainMenu.setFullScreenMode(true);
    }

    /**
     * Called when App is started or resumed
     */
    public void startApp() {

        System.out.println("startApp()");
    }

    /**
     * Called when App is paused
     */
    public void pauseApp() {

        canvas.pause(); // Pause game also
    }

    public void destroyApp(boolean unconditional) {
        
    }

    /**
     * Called when app should be closed
     */
    private void exitMIDlet() {

        display.setCurrent(null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Getter for current display object
     * @return
     */
    public static Display getDisplay() {

        return display;
    }

    /**
     * Getter for current TawaiMain mainclass object
     * @return
     */
    public static TawaiMain getMain() {

        return canvas;
    }


    /**
     * Class is responsible for rendering game main menu
     */
    private class MenuCanvas extends Canvas {

        /**
         * Font used for game title
         */
        private Font titleFont;

        /**
         * Vector of menu items, which are buttons
         */
        private Vector items;

        /**
         * Selected item's index
         */
        private int selectedIndex;

        /**
         * Constructor<br>
         */
        public MenuCanvas () {

            items = new Vector();
            titleFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE);
            items.addElement(new MenuItem("Start", titleFont, 0, 50));
            items.addElement(new MenuItem("Exit", titleFont, 1, 100));
            selectedIndex = 0;
        }

        /**
         * Method renders menu
         * 
         * @param g {@link Graphics} object to draw on
         */
        protected void paint(Graphics g) {

            // Background
            g.setColor(0xFF4B2B);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());

            // Title
            g.setColor(0xFFDD00);
            g.setFont(titleFont);
            g.drawString("TANKS", this.getWidth()/2, 20, Graphics.BASELINE | Graphics.HCENTER);

           // Menu items
            for(int i = 0; i<items.size(); i++) 
                ((MenuItem)items.elementAt(i)).paint(g, (i == selectedIndex));

        }

        /**
         * Processed key presses
         * 
         * @param keyCode Code of pressed button
         */
        protected void keyPressed(int keyCode) {
            super.keyPressed(keyCode);

            keyCode = getGameAction(keyCode);

            // Handling menu items traversing
            switch (keyCode) {
                case Canvas.UP:
                    selectedIndex--;
                    if (selectedIndex == -1)
                        selectedIndex = 0;
                    break;
                case Canvas.DOWN:
                        selectedIndex++;
                    if (selectedIndex == items.size())
                        selectedIndex = items.size() - 1;
                    break;
                case Canvas.FIRE:
                    executeAction(((MenuItem) items.elementAt(selectedIndex)).getIndex());
                    break;
            }

            repaint();
        }

        /**
         * Called when action assigned to menu items should be executed<br>
         * TODO This mechanism should be reimplemented because it is weakly scalable for now
         *
         * @param index Index of action as assigned to menu item
         */
        private void executeAction(int index) {

            switch (index) {
                case 0:
                    try {
                        display.setCurrent(canvas);
                        canvas.initialize();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    break;
                case 1:
                    exitMIDlet();
                    break;
            }
        }

        /**
         * Class models menu item
         * Double-nested class
         */
        private class MenuItem {

            /**
             * Item's properties
             */
            private int width;
            private int height;
            private String label;
            private Font font;
            
            /**
             * Action index. Used by MenuCanvas.executeAction()
             */
            public int index;

            /**
             * y-coordinate on the screen
             */
            private int y;

            /**
             * Constructor
             *
             * @param initLabel Menu item's text label
             * @param font Label's font
             * @param initIndex Action index
             * @param initY y-coordinate on the screen
             */
            public MenuItem (String initLabel, Font initFont, int initIndex, int initY) {

                label = initLabel;
                index = initIndex;
                y = initY;
                font = initFont;
                height = font.getHeight() + 15;
                width = font.stringWidth(label) + 20;
            }

            /**
             * Paints menu item on specified {@link Graphics} object
             *
             * @param gr {@link Graphics} object to draw on
             * @param selected Determines either menu item should be rendered as selected or not
             */
            public void paint(Graphics gr, boolean selected) {

                if (selected) 
                    gr.setColor(0xFFDD00);
                else
                    gr.setColor(0x000000);
                gr.fillRect(gr.getClipWidth()/2 - width/2, y, width, height);

                gr.setFont(font);

                if (selected)
                    gr.setColor(0x000000);
                else
                    gr.setColor(0xFFDD00);
                gr.drawString(label, gr.getClipWidth()/2, y + height/2, Graphics.BASELINE | Graphics.HCENTER);
            }

            /**
             * Getter for menu item's action index
             * @return Action index
             */
            public int getIndex() {

                return index;
            }
        }// end of MenuItem nested class

    } // end of MenuCanvas nested class


}
